﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using System.Web.Services;
using Eb.MessageBox;
public partial class ucUserRegister : System.Web.UI.UserControl
{
    UserManager _userRegister = new UserManager();
    
   
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSubmit.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSubmit.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        if (Session["UserManager_Edit"] != "User Edit")
        {
            string condition = "UD_UserPersonalID IN (SELECT  MAX(UD_UserPersonalID) FROM UserDetails) ";
            DataSet ds = _userRegister.GetData("UserDetails", condition);
            Session["UserManager_PersonalID"] = ds.Tables[0].Rows[0]["UD_UserPersonalID"];
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Submit();
    }

    void Submit()
    {
        //if (txtUserName.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter UserName')</script>");
        //    return;
        //}
        //else if (txtPassword.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter Password')</script>");
        //    return;
        //}
        //else if (txtRePassword.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Re-Enter Password')</script>");
        //    return;
        //}
        //else if (txtAuthenticateQuestion.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter Authentication Question')</script>");
        //    return;
        //}
        //else if (txtAuthenticateAnswer.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter Authentication Answer')</script>");
        //    return;
        //}
        //else if (txtPassword.Text != txtRePassword.Text)
        //{
        //    Response.Write("<script>alert('Passwords Do Not Match')</script>");
        //    return;
        //}
        try
        {
            UserDetails userLog = new UserDetails();
            //userLog.UserID = Session["UserManager_PersonalID"].ToString();
            if (Session["UserManager_Edit"] != "User Edit")
                userLog.UserID = Session["UserManager_PersonalID"].ToString();
            else
                userLog.UserID = ((string[])Session["UserManager_IDs"])[0];
            userLog.UserFirstName = txtUserName.Text;
            userLog.Password = txtPassword.Text;
            userLog.AuthenticationQuestion = txtAuthenticateQuestion.Text;
            userLog.AuthenticationAnswer = txtAuthenticateAnswer.Text;
            //string str = _userRegister.UserRegister(userLog);
            //Response.Write(str);
            if (_userRegister.UserRegister(userLog))
            {
                MessageBox.Show("Successfully Registered.");
               // Response.Write("<script>alert('Registered Successfully !')</script>");
                Clear();
            }
            else
            {
                MessageBox.Show("Username already exist!");
               // Response.Write("<script>alert('User Name Already Exist !')</script>");
            }

        }

        catch (Exception exp)
        {
            Session["ERRTITLE"] = "User Register Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }


    void Clear()
    {
        txtUserName.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtRePassword.Text = string.Empty;
        txtAuthenticateQuestion.Text = string.Empty;
        txtAuthenticateAnswer.Text = string.Empty;
    }
    protected void lnbUsers_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserSearch.aspx");
    }
    protected void lnbRoles_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserRoleSearch.aspx");
    }

    protected void lnbUtilities_Click(object sender, EventArgs e)
    {
        Response.Redirect("Utility Search.aspx");
    }
    protected void lnbPermissions_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddPermission.aspx");
    }
    protected void lnbHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserHome.aspx");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Clear();
        Session["UserManager_Control"] = "UserManager/ucUserAdd.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
}
